﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.IICMoudle
{
    public class I2CProxy
    {
        public enum ChipType
        {
            // 芯海
            CHIP_SEA = 1,

            // ST
            CHIP_ST = 2,

            // 美信
            CHIP_MAXIM = 3,

            // 艾为
            CHIP_AW = 4
        }

        private static I2CProxy instance;
        private static readonly object syncObj = new object();

        public static IChip chip;

        private I2CProxy()
        { }

        public static I2CProxy GetInstace(int type)
        {
            if (null == instance)
            {
                lock (syncObj)
                {
                    if (null == instance)
                    {
                        instance = new I2CProxy();
                        chip = GetChip(type);
                    }
                }
            }
            return instance;
        }

        private static IChip GetChip(int type)
        {
            if (type == (int)ChipType.CHIP_ST)
            {
                return new ChipSt();
            }
            else if (type == (int)ChipType.CHIP_MAXIM)
            {
                return new ChipMaxim();
            }
            else if (type == (int)ChipType.CHIP_AW)
            {
                return new ChipAw();
            }
            else
            {
                return new ChipSea();
            }
        }

        private bool opened = false;

        public int CH_NUM { get; set; }

        public bool Open()
        {
            if (!opened)
            {
                opened = IIC.Init();
            }
            return opened;
        }

        public void Close()
        {
            if (opened)
            {
                IIC.Close();
            }
        }

        public void SetIICSpeed(int speed)
        {
            IIC.SetIICSpeed(speed);
        }

        public void WakeUp()
        {
            byte[] data = new byte[] { 0x00 };
            ReadReg(data, 2);
        }

        public bool WriteReg(byte[] datas, int len)
        {
            return chip.WriteReg(datas, len);
        }

        public byte[] ReadReg(byte[] addrBuf, int len)
        {
            return chip.ReadReg(addrBuf, len);
        }
        
        public void SetDebugMode(byte mode)
        {
            chip.SetDebugMode(mode);
        }

        public void ClearDataReady()
        {
            chip.ClearDataReady();
        }

        public int ReadDataReady()
        {
            return chip.ReadDataReady();
        }

        /// <summary>
        /// 输出所有数据包括校验值
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public int[] ReadData(int len)
        {
            return chip.GetData(len);
        }

        public string ReadFwVersion()
        {
            return chip.ReadFwVersion();
        }

        internal string ReadModuleConnectDynamic()
        {
            return chip.ReadModuleConnectDynamic();
        }

        internal short ReadModuleConnect()
        {
            return chip.ReadModuleConnect();
        }

        internal UpgradeResult UpdateFw(string filePath)
        {
            return chip.UpdateFW(filePath);
        }

        internal byte[] ReadAfeGain()
        {
            return chip.ReadAfeGain();
        }

        internal short[] ReadOffset(int chNum)
        {
            
            return chip.ReadOffset(chNum);
        }
        
        public short ReadDacToMvCoef()
        {
            
            return chip.ReadDacToMvCoef();
            
            
        }

        internal int ReadAdcToUvCoef()
        {
            return chip.ReadAdcToUvCoef();
        }

        internal byte WriteCoeff(float[] coefficient)
        {
            return chip.WriteCoeff(coefficient);
        }

        internal float[] ReadCoeff(int chNum)
        {
            return chip.ReadCoeff(chNum);
        }
    }
}
